package com.chookss.wxapi;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.home.entity.ShareEntity;
import com.chookss.home.rank.RankLinkEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.ShareCodeDialog;
import com.chookss.view.ShareQrCodeDialog;
import com.chookss.view.listener.ShareDialogListener;
import com.google.gson.reflect.TypeToken;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.Nullable;
import io.reactivex.functions.Consumer;

public class ShareActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.common_right_txt)
    TextView commonRightTxt;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvInit)
    TextView tvInit;
    @BindView(R.id.tvInit2)
    TextView tvInit2;
    @BindView(R.id.tvPlusChoice)
    TextView tvPlusChoice;
    @BindView(R.id.tvChoice)
    TextView tvChoice;
    @BindView(R.id.edTitle)
    EditText edTitle;
    @BindView(R.id.edContent)
    EditText edContent;
    @BindView(R.id.ivChoice)
    ImageView ivChoice;
    @BindView(R.id.ivChoice2)
    ImageView ivChoice2;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ivWechat)
    ImageView ivWechat;
    @BindView(R.id.ivCode)
    ImageView ivCode;
    @BindView(R.id.ivCopy)
    ImageView ivCopy;
    @BindView(R.id.ivShareCode)
    ImageView ivShareCode;
    @BindView(R.id.ivDown)
    ImageView ivDown;
    @BindView(R.id.ivBg)
    View ivBg;
    @BindView(R.id.ivPlusBg)
    View ivPlusBg;
    @BindView(R.id.ivPlusChoice)
    ImageView ivPlusChoice;
    @BindView(R.id.ivPlusChoice2)
    ImageView ivPlusChoice2;
    @BindView(R.id.llChoice)
    RelativeLayout llChoice;
    @BindView(R.id.llPlusChoice)
    RelativeLayout llPlusChoice;
    private MyAdapter adapter;
    private List<ShareEntity> list = new ArrayList<>();
    private String resourceId, title, content, picUrl, time, size, videoShareCounts;//
    private int resourceType = 1;//（1资料，2长视频，3短视频，4新闻5图文）
    private int subscribeFlag = 0;//0不限制 1需关注 2需会员 4关注+会员)
    private Bitmap bitmapImg = null;
    private int index = -1;//选择话术列表下标
    boolean downloadFlag = true;
    boolean shareFlag = true;
    private String shareUrl = "";
    private String createEmployeeName = "";//分享的资源创建者
    private String ownerPrefix = "";//是否因为域名问题不能分享 1可以分享 0不可以分享
    private int errorCode = 0;//1000的时候表示域名异常
    private RankLinkEntity rankLinkEntity = null;//溯源分享链接过来的
    private int sourceType = 0;//0普通来源分享 1溯源分享链接过来的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        commonTitleTxt.setText("分享");
        sourceType = getIntent().getIntExtra("sourceType", 0);
        createEmployeeName = getIntent().getStringExtra("createEmployeeName");
        title = getIntent().getStringExtra("title");
        content = getIntent().getStringExtra("content");
        picUrl = getIntent().getStringExtra("picUrl");
        time = getIntent().getStringExtra("time");
        size = getIntent().getStringExtra("size");
        videoShareCounts = getIntent().getStringExtra("videoShareCounts");
        resourceId = getIntent().getStringExtra("resourceId");
        resourceType = getIntent().getIntExtra("resourceType", 4);
        tvTitle.setText(title);
        edTitle.setText(title);
        if (sourceType == 1) {
            //我的溯源过来的
            rankLinkEntity = (RankLinkEntity) getIntent().getSerializableExtra("rankLinkData");
            createEmployeeName = new ShareUtils().getString(this, StaticClass.USER_NICKNAME, "");
            resourceType = Utils.changeNumber(rankLinkEntity.getResourceType());
            resourceId = rankLinkEntity.getResourceId();
            tvTitle.setText(rankLinkEntity.getResourceName());
            edTitle.setText(rankLinkEntity.getResourceName());
            title = rankLinkEntity.getResourceName();
            content = rankLinkEntity.getRemarks();
            if (!Utils.isNull(rankLinkEntity.getRemarks())) {
                edContent.setText(rankLinkEntity.getRemarks());
            }
        }
        if (resourceType == 1) {
            Glide.with(this).load(R.drawable.iv_datas_300).into(iv);
            tvInit.setText(size);
            if (!Utils.isNull(content)) {
                edContent.setText(content);
            } else {
                edContent.setHint("这里是资料内容描述");
            }

        } else if (resourceType == 2 || resourceType == 3) {
            KLog.i(time + "==" + Utils.changeTime(time));
            GlideUtils.loadCustomRound(this, picUrl, iv, 4);
            if (!Utils.isNull(content)) {
                edContent.setText(content);
            }
            tvInit.setText(Utils.changeTime(time));
            edContent.setHint("这里是视频内容描述");
            if (!Utils.isNull(picUrl)) {
                try {
                    Glide.with(this).asBitmap()
                            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).load(picUrl)
                            .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) {
                                    try {
                                        if (resource != null) {
                                            bitmapImg = resource;
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            if (Utils.isNull(picUrl)) {
                Glide.with(this).load(R.drawable.iv_news_300).into(iv);
            } else {
                GlideUtils.loadCustomRound(this, picUrl, iv, 4);
            }
            if (!Utils.isNull(picUrl)) {
                try {
                    Glide.with(this).asBitmap()
                            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).load(picUrl)
                            .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) {
                                    try {
                                        if (resource != null) {
                                            bitmapImg = resource;
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            tvInit.setVisibility(View.GONE);
            edContent.setHint("这里是新闻内容描述");
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new MyAdapter(R.layout.item_share, this, list);
        recyclerView.setAdapter(adapter);
        if (sourceType == 1) {
            tvPlusChoice.setTextColor(Color.parseColor("#999999"));
            tvChoice.setTextColor(Color.parseColor("#999999"));
        } else {
            getData();
        }
        getPermissions();
    }

    private void getData() {
        showLoading();
        Map<String, String> map = new HashMap<>();
        map.put("resourceId", resourceId);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<ShareEntity>>>() {
        }.getType(), null, Urls.getBestShare, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<ShareEntity>>() {
            @Override
            public void onSuccess(List<ShareEntity> data) {
                dismissLoading();
                list.clear();
                if (data != null && data.size() > 0) {
                    list.addAll(data);
                    adapter.notifyDataSetChanged();
                }
                if (list.size() > 0) {
                    tvInit2.setTextColor(Color.parseColor("#E01F1A"));
                    tvInit2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); //下划线
                    tvInit2.getPaint().setAntiAlias(true);//抗锯齿
                } else {
                    tvInit2.setTextColor(Color.parseColor("#999999"));
                }
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    private void getPermissions() {
        Map<String, String> map = new HashMap<>();
        map.put("code", resourceId);
        //1视频2资料3新闻4图文
        if (resourceType == 1) {
            map.put("type", "2");
        } else if (resourceType == 4) {
            map.put("type", "3");
        } else if (resourceType == 5) {
            map.put("type", "4");
        } else {
            map.put("type", "1");
        }
        MyHttpRequest.getRequest(Urls.selsectShareFlag, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                dismissLoading();
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        errorCode = 200;
                        JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                        if (jsonObject1.has("ownerPrefix")) {
                            ownerPrefix = jsonObject1.getString("ownerPrefix");
                        }
                        if (resourceType != 4) {
                            if ("1".equals(jsonObject1.getString("downloadFlag"))) {
                                downloadFlag = true;
                            } else {
                                downloadFlag = false;
                            }
                        }

                        if ("1".equals(jsonObject1.getString("shareFlag"))) {
                            shareFlag = true;
                        } else {
                            shareFlag = false;
                        }
                        if (!shareFlag) {
                            ivWechat.setBackgroundResource(R.drawable.icon_wechat_no_permissions);
                            ivCode.setBackgroundResource(R.drawable.icon_erweima_no_permissions);
                            ivCopy.setBackgroundResource(R.drawable.icon_copy_no_permissions);
                        }
                        if (!downloadFlag) {
                            ivDown.setBackgroundResource(R.drawable.icon_xiazai_no_permissions);
                        }
                        if (sourceType == 1) {
                            if (!"1".equals(rankLinkEntity.getStatusCd())) {
                                //关闭分享功能了
                                shareFlag = false;
                                ivWechat.setBackgroundResource(R.drawable.icon_wechat_no_permissions);
                                ivCode.setBackgroundResource(R.drawable.icon_erweima_no_permissions);
                                ivCopy.setBackgroundResource(R.drawable.icon_copy_no_permissions);
                            } else {
                                shareFlag = true;
                            }
                        }
                    } else {
                        errorCode = 1000;
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"), jsonObject.getString("msg"));
                        } else {
                            MyToast.show(jsonObject.getString("msg"));
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void saveData(int num, String locTitle, String locContent) {
        if (resourceType == 1) {
            shareDocunment();
        } else if (resourceType == 2 || resourceType == 3) {
            shareVideo();
        }
        Map<String, String> map = new HashMap<>();
        map.put("resourceId", resourceId);
        map.put("resourceType", resourceType + "");
        map.put("title", locTitle);
        if (!Utils.isNull(locContent)) {
            map.put("descContent", locContent);
        }
//        map.put("descContent", "描述");
        map.put("subscribeFlag", subscribeFlag + "");
        if (!Utils.isNull(createEmployeeName)) {
            map.put("createEmployeeName", createEmployeeName);
        }
        MyHttpRequest.postRequest(Urls.wxShareInsert, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                dismissLoading();
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        if (Utils.isNull(jsonObject.getString("data"))) {
                            MyToast.show("服务器异常，请稍后重试");
                            return;
                        }
                        JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                        if (jsonObject1.has("url")) {

                            if (num == 0) {
                                shareUrl = jsonObject1.getString("wxUrl");
                            } else if (num == 1) {
                                shareUrl = jsonObject1.getString("qrUrl");
                            } else {
                                shareUrl = jsonObject1.getString("linkUrl");
                            }
                            toShare(num, locTitle, locContent);
                        }

                    } else if ("500".equals(jsonObject.getString("code"))) {
                        MyToast.show("内容错误，仅支持输入文字");
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"), jsonObject.getString("msg"));
                        } else {
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void shareDocunment() {
        Map<String, String> map = new HashMap<>();
        map.put("dataCode", resourceId);
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<String>>() {
        }.getType(), null, Urls.databaseShareTime, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    private void shareVideo() {
        Map<String, String> map = new HashMap<>();
        map.put("shareCode", resourceId);
        map.put("employeeCode", new ShareUtils().getString(this, StaticClass.EMPLOYEECODE, ""));
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<String>>() {
        }.getType(), null, Urls.shareVideo, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                HashMap<String, String> map = new HashMap();
                map.put("videoId", resourceId);
                map.put("videoShareCounts", Utils.addNumber(videoShareCounts));
                EventBus.getDefault().post(new MyEvent(map));
                Map<String, String> map2 = new HashMap<>();
                map2.put("videoId", resourceId);
                map2.put("videoShareCounts", Utils.addNumber(videoShareCounts));
                EventBus.getDefault().post(new MyEvent(map2));
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    private void toGetUrl(int num) {
        if (errorCode == 1000) {
            MyToast.show("需管理员配置专属域名后可分享");
            return;
        }
        if (!Utils.isNull(ownerPrefix)) {
            if (ownerPrefix.equals("0")) {
                MyToast.show("需管理员配置专属域名后可分享");
                return;
            }
        }
        if (num == 0) {
            if (!shareFlag) {
                MyToast.show("作者已关闭分享功能");
                return;
            }
        } else if (num == 1) {
            if (!shareFlag) {
                MyToast.show("作者已关闭分享功能");
                return;
            }
        } else if (num == 2) {
            if (!shareFlag) {
                MyToast.show("作者已关闭分享功能");
                return;
            }
        } else if (num == 3) {
        } else if (num == 4) {
            if (!downloadFlag) {
                MyToast.show("作者已关闭下载功能");
                return;
            }
        }
        String newTitle="", newContent="";
        if (index > -1) {
            newTitle = list.get(index).getTitle();
            newContent = list.get(index).getDescContent();
            shareUrl = list.get(index).getUrl();
            if (resourceType == 1) {
                shareDocunment();
            } else if (resourceType == 2 || resourceType == 3) {
                shareVideo();
            }
            toShare(num, newTitle, newContent);
        } else {
            if (!Utils.isNull(edTitle.getText().toString())) {
                newTitle = edTitle.getText().toString();
            } else {
                newTitle = title;
            }
            if (!Utils.isNull(edContent.getText().toString())) {
                newContent = edContent.getText().toString();
            }
//            else {
//                newContent = content;
//            }
            saveData(num, newTitle, newContent);
        }

    }

    private void toShare(int num, String locTitle, String locContent) {
        if (num == 0) {
            if (!JustWeChatExis.isWeChatAppInstalled(this)) {
                MyToast.show("您的设备未安装微信客户端");
                return;
            }
            if (bitmapImg == null) {
                if (resourceType == 1) {
                    bitmapImg = BitmapFactory.decodeResource(getResources(), R.drawable.iv_datas_300);
                } else if (resourceType == 2 || resourceType == 3) {
                    bitmapImg = BitmapFactory.decodeResource(getResources(),GlideUtils.loadPushId(ShareActivity.this));
                } else if (resourceType == 4) {
                    bitmapImg = BitmapFactory.decodeResource(getResources(), R.drawable.iv_news_300);
                } else {
                    bitmapImg = BitmapFactory.decodeResource(getResources(), GlideUtils.loadPushId(ShareActivity.this));
                }

            }
            WeChatShareUtil.getInstance(this).shareUrl(shareUrl, locTitle, bitmapImg, locContent, SendMessageToWX.Req.WXSceneSession);
            Observable.timer(1500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) {
                            finish();
                        }
                    });
        } else if (num == 1) {
            new ShareQrCodeDialog(ShareActivity.this, locTitle, shareUrl,
                    new ShareDialogListener() {
                        @Override
                        public void onClick() {
                            Observable.timer(1500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<Long>() {
                                        @Override
                                        public void accept(Long aLong) {
                                            finish();
                                        }
                                    });
                        }
                    }).show();
        } else if (num == 2) {
            ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            clipboardManager.setPrimaryClip(ClipData.newPlainText("", shareUrl));
            MyToast.show("已复制到剪切板");
            Observable.timer(1500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) {
                            finish();
                        }
                    });
        } else if (num == 3) {
            new ShareCodeDialog(ShareActivity.this, resourceId,
                    new ShareDialogListener() {
                        @Override
                        public void onClick() {
                            ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            clipboardManager.setPrimaryClip(ClipData.newPlainText("", resourceId));
                            MyToast.show("已复制到剪切板");
                            Observable.timer(1500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<Long>() {
                                        @Override
                                        public void accept(Long aLong) {
                                            finish();
                                        }
                                    });
                        }
                    }).show();
        } else if (num == 4) {
        }
    }

    @OnClick({R.id.common_title_back, R.id.tvInit2, R.id.llPlusChoice, R.id.llWechat, R.id.llCode, R.id.llCopy, R.id.llShareCode, R.id.llDown, R.id.llChoice})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvInit2:
                if (sourceType == 0) {
                    //只有普通分享下才可以选择  溯源过来的不能选择
                    if (list.size() > 0) {
                        recyclerView.setVisibility(View.VISIBLE);
                    } else {
                        recyclerView.setVisibility(View.GONE);
                    }
                }
                break;
            case R.id.llWechat:
                toGetUrl(0);
                break;
            case R.id.llCode:
                toGetUrl(1);
                break;
            case R.id.llCopy:
                toGetUrl(2);
                break;
            case R.id.llShareCode:
                toGetUrl(3);
                break;
            case R.id.llDown:
                toGetUrl(4);
                break;
            case R.id.llChoice:
                if (sourceType == 0) {
                    //只有普通分享下才可以选择  溯源过来的不能选择
                    if (index > -1) {
                        MyToast.show("选择最优话术后不能操作");
                    } else {
                        initChoice(0);
                    }
                }
                break;
            case R.id.llPlusChoice:
                if (sourceType == 0) {
                    //只有普通分享下才可以选择   溯源过来的不能选择
                    if (index > -1) {
                        MyToast.show("选择最优话术后不能操作");
                    } else {
                        initChoice(1);
                    }
                }
                break;
        }
    }

    private void initChoice(int index) {
//        subscribeFlag 浏览限制标识(0不限制 1需关注 2需会员 4关注+会员)
        if (index == 0) {
            if (subscribeFlag == 0) {
                subscribeFlag = 1;
            } else if (subscribeFlag == 1) {
                subscribeFlag = 0;
            } else if (subscribeFlag == 2) {
                subscribeFlag = 4;
            } else if (subscribeFlag == 4) {
                subscribeFlag = 2;
            }
        } else {
            if (subscribeFlag == 0) {
                subscribeFlag = 2;
            } else if (subscribeFlag == 1) {
                subscribeFlag = 4;
            } else if (subscribeFlag == 2) {
                subscribeFlag = 0;
            } else if (subscribeFlag == 4) {
                subscribeFlag = 1;
            }
        }
        setChoiceInit();
    }

    private void setChoiceInit() {
        if (subscribeFlag == 0) {
            dismissFocus();
            dismissPlus();
        } else if (subscribeFlag == 1) {
            showFocus();
            dismissPlus();
        } else if (subscribeFlag == 2) {
            dismissFocus();
            showPlus();
        } else if (subscribeFlag == 4) {
            showFocus();
            showPlus();
        }
    }

    private void showPlus() {
        //显示会员
        ivPlusBg.setVisibility(View.VISIBLE);
        ivPlusChoice.setVisibility(View.VISIBLE);
        ivPlusChoice2.setVisibility(View.GONE);
    }

    private void dismissPlus() {
        //取消会员
        ivPlusBg.setVisibility(View.GONE);
        ivPlusChoice.setVisibility(View.GONE);
        ivPlusChoice2.setVisibility(View.VISIBLE);
    }

    private void showFocus() {
        //显示关注
        ivBg.setVisibility(View.VISIBLE);
        ivChoice.setVisibility(View.VISIBLE);
        ivChoice2.setVisibility(View.GONE);
    }

    private void dismissFocus() {
        //取消关注
        ivBg.setVisibility(View.GONE);
        ivChoice.setVisibility(View.GONE);
        ivChoice2.setVisibility(View.VISIBLE);
    }

    class MyAdapter extends BaseQuickAdapter<ShareEntity, BaseViewHolderEx> {
        private Context context;

        public MyAdapter(int layoutResId, Context context, @Nullable List<ShareEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, ShareEntity item) {

            TextView tvNum = helper.getView(R.id.tvNum);
            TextView tvTitle = helper.getView(R.id.tvTitle);
            TextView tvContent = helper.getView(R.id.tvContent);
            TextView tvInit = helper.getView(R.id.tvInit);
            RelativeLayout rlShare = helper.getView(R.id.rlShare);
            View ivBg = helper.getView(R.id.ivBg);
            ImageView ivChoice = helper.getView(R.id.ivChoice);
            ImageView ivChoice2 = helper.getView(R.id.ivChoice2);
            tvNum.setText((helper.getItemPosition() + 1) + "");
            tvTitle.setText(item.getTitle());
            tvContent.setText(item.getDescContent());
            if (!Utils.isNull(item.getCreateTime())) {
                if (item.getCreateTime().length() > 10) {
                    tvInit.setText(item.getCreateUser() + "在" + item.getCreateTime().substring(0, 10) + "创建      浏览" + item.getViewNum() + "次");

                } else {
                    tvInit.setText(item.getCreateUser() + "在" + item.getCreateTime() + "创建      浏览" + item.getViewNum() + "次");

                }
            }
            if (index == helper.getItemPosition()) {
                ivBg.setVisibility(View.VISIBLE);
                ivChoice.setVisibility(View.VISIBLE);
                ivChoice2.setVisibility(View.GONE);
            } else {
                ivBg.setVisibility(View.GONE);
                ivChoice.setVisibility(View.GONE);
                ivChoice2.setVisibility(View.VISIBLE);
            }
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    edTitle.setText(item.getTitle());
                    edContent.setText(item.getDescContent());
                }
            });
            rlShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!shareFlag) {
                        MyToast.show("暂不能分享");
                        return;
                    }
                    if (index == helper.getItemPosition()) {
                        index = -1;
                        adapter.notifyItemChanged(helper.getItemPosition());
                    } else {
                        int oldIndex = index;
                        index = helper.getItemPosition();
                        adapter.notifyItemChanged(oldIndex);
                        adapter.notifyItemChanged(index);
                    }
                    if (index > -1) {
                        edTitle.setEnabled(false);
                        edContent.setEnabled(false);
                        edTitle.setTextColor(Color.parseColor("#bcbcbc"));
                        edContent.setTextColor(Color.parseColor("#bcbcbc"));
                        subscribeFlag = Utils.changeNumber(item.getSubscribeFlag());
                        setChoiceInit();
                    } else {
                        edTitle.setEnabled(true);
                        edContent.setEnabled(true);
                        edTitle.setTextColor(Color.parseColor("#666666"));
                        edContent.setTextColor(Color.parseColor("#666666"));
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (edContent != null) {
                Utils.hideInputKeyboard(this, edContent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
